package com.wenx.restcamel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.restlet.RestletConstants;
import org.restlet.data.Form;
import org.restlet.engine.adapter.HttpRequest;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RestletProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {

        /*String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
        String queryString1 = "id=1&type=cmcc&name=admin";
        Map<String, String> map
                = Stream.of(queryString.split("&"))
                    .map(obj -> obj.split("="))
                    .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));*/

        HttpRequest req = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, HttpRequest.class);

        Form form = req.getResourceRef().getQueryAsForm();

        //这个map就是url上面的所有参数
        Map<String, String> maps = form.getValuesMap();

        System.out.println(maps.get("id"));//获取id参数
        System.out.println(maps.get("name"));//获取type参数

        exchange.getOut().setBody("this is out body");
    }
}
